package com.zfjt.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00014;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.utils.StringUtil;
import com.zfjt.utils.WQUtil;

/**
 * 传传电话流水照片接口
 * @author xk
 * @version 1.0
 * @date 2017年4月06日
 */
@Controller
public class AddImg06Controller {
	
	private ReturnBean mReturn;
	
	private WQUtil wqUtil;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	private Map<String, String> fhdata; 
	
	@RequestMapping(value="00014")
	@ResponseBody
	public ReturnBean AddImg06(HttpSession hseesion,HttpServletRequest request){
		wqUtil = new WQUtil();
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00014 num00014 = JSON.parseObject(data,PackNum00014.class); 	
		ClientmessageBean mdata = mClientMessage.selectByPrimaryKey((short) Integer.parseInt(num00014.getId()));
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00014");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{	
			//获取Tomcat下项目根目录
			String path = request.getSession().getServletContext().getRealPath("static\\images" + "/") +"\\" + mdata.getName();
			//根据用户名新建文件夹存相应照片
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_01())) {
				wqUtil.GenerateImage(num00014.getImg06_01(),path + "\\" + "Img06_01" + ".jpg");
				mdata.setImg0601(path + "\\" + "Img06_01" + ".jpg");
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_02())) {
				wqUtil.GenerateImage(num00014.getImg06_02(),path + "\\" + "Img06_02" + ".jpg");
				mdata.setImg0602(path + "\\" + "Img06_02" + ".jpg");
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_03())) {
				wqUtil.GenerateImage(num00014.getImg06_03(),path + "\\" + "Img06_03" + ".jpg");
				mdata.setImg0603(path + "\\" + "Img06_03" + ".jpg");
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_04())) {
				wqUtil.GenerateImage(num00014.getImg06_04(),path + "\\" + "Img06_04" + ".jpg");
				mdata.setImg0604(path + "\\" + "Img06_04" + ".jpg");
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_05())) {
				wqUtil.GenerateImage(num00014.getImg06_05(),path + "\\" + "Img06_05" + ".jpg");
				mdata.setImg0605(path + "\\" + "Img06_05" + ".jpg");
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_06())) {
				wqUtil.GenerateImage(num00014.getImg06_06(),path + "\\" + "Img06_06" + ".jpg");
				mdata.setImg0606(path + "\\" + "Img06_06" + ".jpg");
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_07())) {
				wqUtil.GenerateImage(num00014.getImg06_07(),path + "\\" + "Img06_07" + ".jpg");
				mdata.setImg0607(path + "\\" + "Img06_07" + ".jpg");
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_08())) {
				wqUtil.GenerateImage(num00014.getImg06_08(),path + "\\" + "Img06_08" + ".jpg");
				mdata.setImg0608(path + "\\" + "Img06_08" + ".jpg");
			}
			if (StringUtil.isNotEmpty(num00014.getImg06_09())) {
				wqUtil.GenerateImage(num00014.getImg06_09(),path + "\\" + "Img06_09" + ".jpg");
				mdata.setImg0609(path + "\\" + "Img06_09" + ".jpg");
			}
			//把完成的接口步骤记录下来
			String str = mdata.getIsaccomplish();
			String reject = mdata.getRejectImg();
			if (num00014.getType().equals("1")) {
				reject=str.replace("img_06,","");
			}
			mdata.setIsaccomplish(str + ",img06");
			if (mdata.getType().equals("4")) {
				mdata.setType("2");
			}
			mClientMessage.updateByPrimaryKeySelective(mdata);
			mReturn.setCode("000");
			fhdata.put("succeed", "1");
			mReturn.setData(JSON.toJSONString(fhdata));
			mReturn.setPortnum("00014");
		}
		return mReturn;
	}
}
